using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text.RegularExpressions;

using CentralOffice.Task.BusinessActions;
using CentralOffice.Enterprise.BusinessActions;
using CentralOffice.File.BusinessActions;
using CentralOffice.Admin.BusinessActions;

using DiaryTable = CentralOffice.MyTable.Data.DiaryDataSet.diaryDataTable;
using DiaryRow = CentralOffice.MyTable.Data.DiaryDataSet.diaryRow;
namespace CentralOffice.WebClient.desktop.diary
{
    public partial class DiaryLeft : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBind(dwNewComment, CentralOffice.MyTable.BusinessActions.Diary.GetPaged("UserId='" + ApplicationContext.ActualUserId + "' and LastCommentTime is not null and deleteflag=0", null, false, " LastCommentTime DESC", 5, 0), dwNewComment_ItemDataBound);
                DataBind(dwShareDiary, CentralOffice.MyTable.BusinessActions.Diary.GetPaged("ToId like '%" + ApplicationContext.ActualUserId + "%' and deleteflag=0", null, false, " DiaDate DESC", 5, 0), dwShareDiary_ItemDataBound);                    
            }
        }

        protected void dwNewComment_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                RepeaterItem item = e.Item;
                ControlCollection cs = item.Controls;
                HyperLink link = e.Item.FindControl("HyperLink1") as HyperLink;

                DiaryRow row = (e.Item.DataItem as DataRowView).Row as DiaryRow;
                if (row != null && !row.IsSubjectNull())
                {
                    link.Text = row.Subject;
                    link.NavigateUrl = "~/desktop/diary/read.aspx?closebuttonflag=no&type=seek&id=" + row.Id.ToString();
                }
            }
        }

        protected void dwShareDiary_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                RepeaterItem item = e.Item;
                ControlCollection cs = item.Controls;
                HyperLink link = e.Item.FindControl("HyperLink1") as HyperLink;

                DiaryRow row = (e.Item.DataItem as DataRowView).Row as DiaryRow;
                link.Text = row.Subject;
                link.NavigateUrl = "~/desktop/diary/read.aspx?closebuttonflag=no&type=share&id=" + row.Id.ToString();
            }
        }


        private void DataBind(DiaryWidget widget, DataTable dt, RepeaterItemEventHandler handler)
        {
            widget.Repeater.ItemDataBound += new RepeaterItemEventHandler(handler);
            widget.Repeater.DataSource = dt;
            widget.Repeater.DataBind();
        }

    }
}
